﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Web.Models;
using KL.Store.Core;
using KL.Store.Core.Domain;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using KL.Store.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using System.Globalization;
using System.Threading;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.StorageClient.Protocol;
using Microsoft.WindowsAzure;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Net;

namespace KL.Store.Web.Controllers
{
    public class ProductManageController : KL.Store.Core.Web.BaseController
    {
        //
        // GET: /ProductManage/

        public ActionResult Index()
        {
            return View();
        }

        #region Main

        public ActionResult Edit(int id = 0, string toEdit = "")
        {
            if (!eStoreUser.IsManager && !eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (string.IsNullOrEmpty(toEdit))
            {
                toEdit = "EditInfo";
                if (!eStoreUser.IsManager)
                {
                    toEdit = "EditMeta";
                }
            }
            if (toEdit != "AddNewProduct")
            {
                ProductMeta pm = dbProduct.GetMeta(id);
                ViewBag.ProductName = pm.ProductName;
                ViewBag.Model = pm.Model;
            }
            ViewBag.id = id;
            ViewBag.ToEdit = toEdit;
            return View();
        }

        #endregion

        #region EditMenu

        public ActionResult EditMenu(int id, string toEdit)
        {
            ViewBag.IsManager = eStoreUser.IsManager;
            ViewBag.IsSeoManager = eStoreUser.IsSeoManager;
            ViewBag.ProductId = id;
            ViewBag.toEdit = toEdit;
            return View();
        }

        #endregion

        #region AddNewProduct

        public ActionResult AddNewProduct(int id = 0, int productId = 0)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.Category = id;
            ProductEditModel productEdit = new ProductEditModel();

            var cacheManager = CacheFactory.GetCacheManager("Cache Manager");
            IList<ProductGroup> brands = (List<ProductGroup>)cacheManager.GetData("BrandsMenu");
            if (brands == null)
            {
                try
                {
                    brands = dbProduct.GetGroupList(Parameters.BrandCategoryId);
                    brands.Insert(0, new ProductGroup { GroupId = 0, GroupName = "Marka Seçiniz" });

                    cacheManager.Add("BrandsMenu", brands, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                    cacheManager.Add("BrandsMenu_Long", brands, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddDays(1)));
                }
                catch
                {
                    cacheManager.Add("BrandsMenu", cacheManager.GetData("BrandsMenu_Long").ToString(), CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                    brands = (List<ProductGroup>)cacheManager.GetData("BrandsMenu");
                }
            }

            IList<CategoryMenuList> categories = new List<CategoryMenuList>();
            if (id == 0)
            {
                categories = (List<CategoryMenuList>)cacheManager.GetData("ProductAddCategoriesMenu");
                if (categories == null)
                {
                    try
                    {
                        categories = dbReportPanel.CategoryMenuListGet().Where(o => o.CategoryType == "A" || o.CategoryType == "D" || o.CategoryType == "S").OrderBy(o => o.CategoryType).ToList();
                        categories.Insert(0, new CategoryMenuList { CategoryId = 0, CategoryName = "Kategori Seçiniz" });

                        cacheManager.Add("ProductAddCategoriesMenu", categories, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                        cacheManager.Add("ProductAddCategoriesMenu_Long", categories, CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddDays(1)));
                    }
                    catch
                    {
                        cacheManager.Add("ProductAddCategoriesMenu", cacheManager.GetData("ProductAddCategoriesMenu_Long").ToString(), CacheItemPriority.High, null, new AbsoluteTime(DateTime.Now.AddMinutes(5)));
                        categories = (List<CategoryMenuList>)cacheManager.GetData("ProductAddCategoriesMenu");
                    }
                }
            }



            productEdit.IsAllowedBuyPrice = eStoreUser.IsAllowedBuyPrice;
            if (productId > 0)
            {
                Product p = new Product();
                p = dbProduct.Get(productId, eStoreUser.AccountId);

                productEdit.ProductName = p.ProductName;
                productEdit.ProductName2 = p.ProductName2;
                productEdit.Description = p.Description;
                productEdit.Width = p.Width;
                productEdit.Length = p.Length;
                productEdit.Height = p.Height;
                productEdit.Brands = brands;
                productEdit.Categories = categories;
                productEdit.SelectedBrand = p.BrandId;
                productEdit.TaxRate = p.TaxRate;
                productEdit.Desi = p.Desi;
                productEdit.GiftString = p.GiftString;
                productEdit.DiscountTotal = p.DiscountTotal;
                productEdit.DiscountPercent = p.DiscountPercent;
                productEdit.DiscountDisplayTotal = p.DiscountDisplayTotal;
                productEdit.IsActive = true;
                productEdit.CheckStock = p.CheckStock;
                productEdit.DeliveryPeriod = p.DeliveryPeriod;
                productEdit.DeliveryTime = p.DeliveryTime;
                productEdit.VarrantyPeriod = p.VarrantyPeriod;
                productEdit.VarrantyTime = p.VarrantyTime;
                productEdit.FastCargo = p.FastCargo;
                productEdit.FreeCargo = p.FreeCargo;
                productEdit.FreeCargoInCity = p.FreeCargoInCity;
                productEdit.FreeInstallmentInCity = p.FreeInstallmentInCity;
                productEdit.InstallmentNeeded = p.InstallmentNeeded;
                productEdit.InstallmentPrice = p.InstallmentPrice;
                productEdit.OpportunityProduct = p.OpportunityProduct;


                productEdit.UstUrunId = productId;

            }
            else
            {
                productEdit.Width = 0;
                productEdit.Length = 0;
                productEdit.Height = 0;
                productEdit.Brands = brands;
                productEdit.Categories = categories;
                productEdit.TaxRate = Parameters.DefaultTaxRate;
                productEdit.Desi = Parameters.DefaultDesi;
                productEdit.IsActive = true;
                productEdit.CheckStock = true;
                productEdit.DeliveryPeriod = "Gün";
                productEdit.VarrantyPeriod = "Yıl";
            }

            return View(productEdit);
        }

        [HttpPost]
        public ActionResult AddNewProduct(int CategoryId, int SelectedBrand, Product product)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (string.IsNullOrEmpty(product.GiftString)) product.GiftString = "";
            if (string.IsNullOrEmpty(product.SpecialText)) product.SpecialText = "";
            if (string.IsNullOrEmpty(product.Description)) product.Description = "";
            if (string.IsNullOrEmpty(product.ProductName2)) product.ProductName2 = "";
            if (string.IsNullOrEmpty(product.Model)) product.Model = "";
            if (string.IsNullOrEmpty(product.ProductName)) { return JavaScript("hataMesaji('İsim alanı boş bırakılamaz!');"); }
            if (CategoryId <= 0) { return JavaScript("hataMesaji('Kategori Seçilmemiş!');"); }
            if (SelectedBrand <= 0) { return JavaScript("hataMesaji('Marka Seçilmemiş!');"); }
            product.Description = System.Web.HttpUtility.HtmlDecode(product.Description);
            string StokTut = product.CheckStock ? "E" : "H";
            if (!eStoreUser.IsAllowedBuyPrice)
            {
                product.DealerSellingPrice = 0;
            }
            int result = dbProduct.AddNewProduct(product, StokTut, CategoryId, SelectedBrand);
            if (result > 0)
            {
                return JavaScript("urunEklendi(" + result + ");");
            }
            else
            {
                return JavaScript("hataMesaji('Kayıt işlemi sırasında bir hata oluştu.');");
            }
        }

        #endregion

        #region EditInfo

        public ActionResult EditInfo(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.ProductId = id;
            return View();
        }

        public ActionResult EditInfoGet(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ProductEditModel productEdit = new ProductEditModel();
            Product product = new Product();
            product = dbProduct.Get2(id);
            Mapper.MapDomainToViewModel2(product, productEdit);
            ViewBag.completeResult = "0";
            productEdit.IsManager = eStoreUser.IsManager;
            productEdit.IsAllowedBuyPrice = eStoreUser.IsAllowedBuyPrice;
            return View(productEdit);
        }

        [HttpPost]
        public ActionResult EditInfoGet(Product product, string completeResult = "0")
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (!string.IsNullOrEmpty(completeResult) && completeResult == "1")
            {
                ViewBag.completeResult = "1";
            }
            else
            {
                ViewBag.completeResult = "0";
            }
            if (!eStoreUser.IsAllowedBuyPrice)
            {
                Product product2 = new Product();
                product2 = dbProduct.Get2(product.ProductId);
                product.DealerSellingPrice = product2.DealerSellingPrice;
            }
            if (string.IsNullOrEmpty(product.GiftString)) product.GiftString = "";
            if (string.IsNullOrEmpty(product.SpecialText)) product.SpecialText = "";
            if (string.IsNullOrEmpty(product.Description)) product.Description = "";
            if (string.IsNullOrEmpty(product.ProductName2)) product.ProductName2 = "";
            if (string.IsNullOrEmpty(product.Model)) product.Model = "";
            if (string.IsNullOrEmpty(product.ProductName)) { return JavaScript("hataMesaji('İsim alanı boş bırakılamaz!');"); }
            product.Description = System.Web.HttpUtility.HtmlDecode(product.Description);
            string StokTut = product.CheckStock ? "E" : "H";
            int result = dbProduct.SaveProduct(product, StokTut);
            if (result == 1)
            {
                return JavaScript("urunKaydedildi('" + completeResult + "');");
            }
            else
            {
                return JavaScript("hataMesaji('Kayıt işlemi sırasında bir hata oluştu.');");
            }
        }


        [HttpPost]
        public ActionResult SetVariantOptionsFromMain(int id)
        {
            int result = dbProduct.SetVariantOptionsFromMain(id);
            if (result == 1)
            {
                return JavaScript("urunKaydedildi('0');");
            }
            else
            {
                return JavaScript("hataMesaji('Kayıt işlemi sırasında bir hata oluştu.');");
            }
        }

        #endregion

        #region EditMeta

        public ActionResult EditMeta(int id)
        {
            if (!eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.ProductId = id;
            return View();
        }

        public ActionResult EditMetaGet(int id)
        {
            if (!eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ProductMetaEditModel productMetaEdit = new ProductMetaEditModel();
            ProductMeta pm = new ProductMeta();
            pm = dbProduct.GetMeta(id);
            Mapper.MapDomainToViewModel2(pm, productMetaEdit);
            ViewBag.completeResult = "0";
            return View(productMetaEdit);
        }

        [HttpPost]
        public ActionResult EditMetaGet(ProductMeta pm, string completeResult = "0")
        {
            if (!eStoreUser.IsSeoManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            pm.WebTitle = !string.IsNullOrEmpty(pm.WebTitle) ? pm.WebTitle : "";
            pm.Description = !string.IsNullOrEmpty(pm.Description) ? pm.Description : "";
            pm.Keywords = !string.IsNullOrEmpty(pm.Keywords) ? pm.Keywords : "";
            pm.CanonicalLink = !string.IsNullOrEmpty(pm.CanonicalLink) ? pm.CanonicalLink : "";
            if (!string.IsNullOrEmpty(completeResult) && completeResult == "1")
            {
                ViewBag.completeResult = "1";
            }
            else
            {
                ViewBag.completeResult = "0";
            }
            int result = dbProduct.SaveProductMeta(pm);
            if (result == 1)
            {
                return JavaScript("urunKaydedildi('" + completeResult + "');");
            }
            else
            {
                return JavaScript("hataMesaji('Kayıt işlemi sırasında bir hata oluştu.');");
            }
        }


        #endregion

        #region ProductProperties

        public ActionResult EditProperties(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.ProductId = id;
            return View();
        }

        public ActionResult EditPropertiesGet(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ProductInfoModel ProductInfoList = new ProductInfoModel();
            ProductInfoList.ProductInfos = dbProductInfo.GetList(id);
            Product p = dbProduct.Get(id, eStoreUser.AccountId);
            ProductInfoList.UstUrunId = p.UstUrunId;
            ProductInfoList.ProductId = id;

            return View(ProductInfoList);
        }

        public ActionResult ProductInfoSave(int orgsatirno, int datasatirno, string datatype, string datainformation, int dataproduct)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProductInfo.EditSingle(orgsatirno, datasatirno, datatype, datainformation, dataproduct);
            return Content(result.ToString());
        }

        public ActionResult ProductInfoInsert(int ProductId, string Type, string Information)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            dbProductInfo.Insert(ProductId, Type, Information);
            return Content("");
        }

        public ActionResult ProductInfoRemove(int ProductId, int SatirNo)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProductInfo.Remove(ProductId, SatirNo);
            return Content(result.ToString());
        }

        public ActionResult ProductPropertiesInsertFromMain(int MainProductId, int SubProductId)
        {

            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProductInfo.ProductPropertiesInsertFromMain(MainProductId, SubProductId);
            return Content(result.ToString());
        }

        [HttpPost]
        public ActionResult UrunExtraAlanGuncelle(string Field, string Value, int ProductId, int SatirNo, string Original)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            var result = dbProductInfo.UpdateExtraField(Field, Value, ProductId, SatirNo, Original);
            return result == 1 ? Content("success") : Content("error");
        }

        #endregion

        #region EditVariant

        public ActionResult EditVariant(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.ProductId = id;
            return View();
        }

        public ActionResult EditVariantGet(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ProductVariantListModel productVariants = new ProductVariantListModel();
            productVariants.VariantList = dbProduct.GetProductVariantListById(id);
            productVariants.VariantList = productVariants.VariantList.OrderBy(o => o.order).ToList();
            productVariants.ProductId = id;
            Product p = dbProduct.Get(id, eStoreUser.AccountId);
            productVariants.UstUrunId = p.UstUrunId;
            return View(productVariants);
        }

        public ActionResult ProductVariantSave(int variantid, int sira, string deger, int dataproduct)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.SaveSingleVariant(variantid, sira, deger, dataproduct);
            return Content(result.ToString());
        }

        public ActionResult ProductVariantRemove(int ProductId, int VariantId)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.RemoveSingleVariant(ProductId, VariantId);
            return Content(result.ToString());
        }

        public ActionResult ProductVariantInsert(int ProductId, string Deger)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.InsertSingleVariant(ProductId, Deger);
            return Content(result.ToString());
        }

        [HttpPost]
        public ActionResult ProductVariantSingleUpdate(int ProductId, int UstUrunId)
        {

            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.UpdateSingleVariant(ProductId, UstUrunId);
            return Content(result.ToString());
        }

        public ActionResult ProductVariantInsertFromMain(int MainProductId, int SubProductId)
        {

            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.ProductVariantInsertFromMain(MainProductId, SubProductId);
            return Content(result.ToString());
        }

        #endregion

        #region EditLinked

        public ActionResult EditLinked(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            LinkedProductsModel Model = new LinkedProductsModel();
            Model.ProductId = id;
            return View(Model);
        }

        public ActionResult EditLinkedGet(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            LinkedProductsModel Model = new LinkedProductsModel();
            Model.ProductId = id;
            Model.Products = dbProduct.GetLinkedProducts(id);
            return View(Model);
        }

        public ActionResult AddLinkedProduct(int ProductId, IEnumerable<int> Products)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (ProductId <= 0 || Products == null || Products.Count() < 1)
            {
                return JavaScript("urunSecilmemis();");
            }
            string eklenenler = "Eklenenler: <br />";
            string eklenemeyenler = "Eklenemeyenler: <br />";
            foreach (var pr in Products)
            {
                int result = dbProduct.SetLinkedProduct(0, ProductId, pr, 0);
                ProductMeta pm = dbProduct.GetMeta(pr);
                if (result > 0)
                {
                    eklenenler += pr.ToString() + " - " + pm.ProductName + "<br />";
                }
                else
                {
                    eklenemeyenler += pr.ToString() + " - " + pm.ProductName + "<br />";
                }
            }
            return JavaScript("bagliUrunEklendi('" + eklenenler + eklenemeyenler + "');");
        }


        [HttpPost]
        public ActionResult RemoveLinkedProduct(int ProductId)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.DeleteLinkedProduct(ProductId);
            if (result == 1)
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }

        #endregion

        #region ProductImages

        public ActionResult EditImages(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.ProductId = id;
            ViewBag.Password = dbRefs.GetRef1Old(1911);
            return View();
        }

        public ActionResult EditImagesGet(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ProductModel pr = new ProductModel();
            Product p = dbProduct.Get((int)id, eStoreUser.AccountId);
            if (p != null)
            {
                Mapper.MapDomainToViewModel(p, pr);
                pr.ProductImages = dbProduct.GetImageListWithoutVariant((int)id);
            }
            ViewBag.Password = dbRefs.GetRef1Old(1911);
            return View(pr);
        }

        #endregion

        #region VariantProductsList

        public ActionResult VariantProductsList(int id, string toEdit)
        {
            VariantListModel ProductList = new VariantListModel();
            ProductList.ProductList = dbProduct.GetVariantListById(id);
            ProductList.ProductId = id;
            ProductList.ToEdit = toEdit;
            return View(ProductList);
        }

        #endregion

        #region EditGroups

        public ActionResult EditGroups(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.ProductId = id;
            return View();
        }

        public ActionResult EditGroupsGet(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ProductGroupListModel ProductGroupList = new ProductGroupListModel();
            ProductGroupList.GroupList = dbProduct.ProductGroupList(id);
            ProductGroupList.ProductId = id;
            return View(ProductGroupList);
        }

        #region GroupsLiveSearch

        [HttpPost]
        public ActionResult GroupsLiveSearch(string search)
        {
            IList<ProductGroups> searchmodel;
            searchmodel = dbProduct.GetGroupLiveSearch(search);

            return Json(new { LiveSearch = searchmodel, ProductCount = searchmodel.Count() }, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region AddCategoriesToProduct & RemoveCategoryFromProduct

        [HttpPost]
        public ActionResult AddCategoriesToProduct(int ProductId, IEnumerable<int> Groups)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (ProductId <= 0)
            {
                return JavaScript("urunSecilmemis();");
            }
            else if (Groups == null || Groups.Count() < 1)
            {
                return JavaScript("grupSecilmemis();");
            }
            string eklenenler = "Eklenenler: <br />";
            string eklenemeyenler = "Eklenemeyenler: <br />";
            foreach (var gr in Groups)
            {
                int result = dbProduct.AddCategoryToProduct(ProductId, gr);
                CategoryMeta cm = dbCategory.GetMeta(gr);
                if (result == 1)
                {
                    eklenenler += gr.ToString() + " - " + cm.CategoryName + "<br />";
                }
                else
                {
                    eklenemeyenler += gr.ToString() + " - " + cm.CategoryName + "<br />";
                }
            }
            return JavaScript("gruplarEklendi('" + eklenenler + eklenemeyenler + "');");
        }


        [HttpPost]
        public ActionResult RemoveCategoryFromProduct(int CategoryId, int ProductId, string key = "")
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            int result = dbProduct.RemoveFromCategory(CategoryId, ProductId);
            if (result == 1)
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }

        #endregion

        #endregion

        #region ProductActiveToogle

        public ActionResult ProductActiveToogle(int id, short changeAll = 0)
        {
            if (!eStoreUser.IsManager)
            {
                return Content("0");
            }
            var result = dbProduct.ProductActiveToogle(id, changeAll);
            return Content(result);
        }

        #endregion

        #region EditStok

        public ActionResult EditStok(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            StockAmountsModel Model = new StockAmountsModel();
            Model.Amounts = dbProduct.GetStockAmounts(id);
            Model.ProductId = id;
            return View(Model);
        }

        #endregion

        #region EditXmlImport

        public ActionResult EditXmlImport(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            StockXmlImportModel Model = new StockXmlImportModel();
            Model.StockXml = dbProduct.GetStockXmlImport(id);
            Model.ProductId = id;
            return View(Model);
        }

        public ActionResult UrunTedarikGuncelle(string Field, string Value, string ProductId, int XmlId, string Original)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            if (Field == "DUZELTAR")
            {
                var dt = new DateTime();
                dt = Convert.ToDateTime(Value);
                Value = dt.ToString("yyyy-MM-dd HH:mm:ss");
            }
            var result = dbProduct.UpdateXmlImportField(Field, Value, ProductId, XmlId, Original);
            return result == 1 ? Content("success") : Content("error");
        }

        #endregion

        #region StockPrices

        public ActionResult EditStockPrices(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            StockPricesModel Model = new StockPricesModel();
            Model.Prices = dbProduct.GetStockPrices(id);
            Model.ProductId = id;
            return View(Model);
        }

        public ActionResult EditStockPrices2(int ProductId, StockPrices Prices)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            var result = dbProduct.SetStockPrices(ProductId, Prices);
            return result == 1 ? JavaScript("urunKaydedildi('0');") : JavaScript("hataMesaji('Kaydedilirken bir hata oluştu.');");
        }

        #endregion

        #region Ürün Sil !!!

        public ActionResult DeleteProduct(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            ViewBag.ProductId = id;
            return View();
        }

        public ActionResult DeleteProductNow(int id)
        {
            if (!eStoreUser.IsManager)
            {
                return Redirect(Parameters.WebSiteUrl);
            }
            Product p = dbProduct.Get(id, eStoreUser.AccountId);
            var redirectUrl = "/";
            if (p.CategoryId > 0)
            {
                Category c = dbCategory.Get(p.CategoryId);
                redirectUrl = c.LinkUrl;
            }
            int result = dbProduct.DeleteProduct(id);
            return result == 1 ? JavaScript("UrunSilindi('" + redirectUrl + "')") : JavaScript("hataMesaji('Ürün silinirken bir hata oluştu.')");
        }

        #endregion

        public ActionResult ClearCache()
        {
            var cacheItemsToClear = (from DictionaryEntry cacheList in HttpRuntime.Cache
                                     let key = cacheList.Key.ToString()
                                    select key).ToList();

            foreach (var key in cacheItemsToClear)
            {
                HttpRuntime.Cache.Remove(key);
            }
            return JavaScript("urunKaydedildi('Cache temizlendi');");
        }
    }
}
